package com.weiiew.backend.controller;

import com.weiiew.backend.utils.ApiResponse;
import com.weiiew.backend.utils.ScriptManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @Description 管理脚本页面的controller类
 * @Author 魏锴
 * @Date 2025/4/29
 */
@RestController
@RequestMapping("/script")
public class ScriptController {
    @Autowired
    private ScriptManager manager;

    @PostMapping("/list")
    public ApiResponse getList() throws IOException {
        return ApiResponse.ok("获取成功").append("list", manager.listScripts());
    }

    @PostMapping("/remove")
    public ApiResponse removeList(String path) throws IOException {
        boolean isDelete = manager.deleteScript(path);
        return ApiResponse.response(isDelete, "删除");
    }

    @PostMapping("/open")
    public ApiResponse openScript(String path) throws IOException {
        return ApiResponse.ok("获取成功").append("script", manager.loadScript(path));
    }
}
